package nd.privatebrowser;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;
import java.security.cert.X509Certificate;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;

import net.htmlparser.jericho.Renderer;

import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.mozilla.universalchardet.UniversalDetector;

public class BrowserUtil {
	
	public static final String TOR_HTTP_PROXY_HOST = "127.0.0.1";
	public static final int TOR_HTTP_PROXY_PORT = 8118;

    public static byte[] loadUrl(URL url) throws IOException {

		Proxy p = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(TOR_HTTP_PROXY_HOST, TOR_HTTP_PROXY_PORT));
		URLConnection conn = url.openConnection(p); 
		if(conn instanceof HttpsURLConnection) {
			((HttpsURLConnection)conn).setHostnameVerifier( new X509HostnameVerifier(){ 
			@Override public boolean verify(String host, SSLSession session) { return true;}  
				
			@Override public void verify(String host, SSLSocket ssl) throws IOException {  }

			@Override public void verify(String host, X509Certificate cert) throws SSLException {  }

			@Override public void verify(String host, String[] cns, String[] subjectAlts) throws SSLException {  } });
		}
		conn.connect();
		InputStream inputStream = conn.getInputStream();        		        		
		
		byte[] b = null;
		if(inputStream != null) {
			ByteArrayOutputStream bytes = new ByteArrayOutputStream();
			
			byte[] buffer = new byte[1024];
			InputStream in = inputStream;
			
			int n;
	        while ((n = in.read(buffer)) != -1) {
	            bytes.write(buffer, 0, n);
	        }
	
        	b = bytes.toByteArray();
        	
		}
		return b;			    			

	}

    public static boolean checkTorConnection() {
    	boolean result = false;
    	try {

    		Socket s = new Socket(TOR_HTTP_PROXY_HOST, TOR_HTTP_PROXY_PORT);
			if( s!= null && s.isConnected()) {
				result = true;
			}
			if(s!= null) {
				s.close();
			}
			
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}    
    
    public static String recognizeEncoding(byte[] buf) {
    	
    	UniversalDetector detector = new UniversalDetector(null);    	
        
        detector.handleData(buf, 0, buf.length);        
        detector.dataEnd();

        String encoding = detector.getDetectedCharset();

        detector.reset();
        
    	if(encoding == null){
    		encoding = "UTF-8";
    	}
    	
		return encoding;
	}
    
    
	public static String renderText(byte[] b) throws UnsupportedEncodingException {
		String result;
		String encoding;	 	        	
		encoding = BrowserUtil.recognizeEncoding(b);

		result = new String(b, encoding);
		
		Renderer r = new TextRenderer(result);
		
		String renderedText=r.toString();
		return renderedText;
	}
	
}
